package com.thinker.checkorder.task;

import com.thinker.checkorder.bean.RepeatParam;
import com.thinker.checkorder.mailutil.SendMailUtil;
import com.thinker.checkorder.service.RepeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * RepeatOrder
 *
 * @author zhougf
 * @date 2019/9/16
 */
@Component
public class RepeatOrder {

    @Autowired
    private RepeatService repeatService;

    private static final DateTimeFormatter FORMAT_FOURTEEN = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    /**定义每过10分钟执行任务*/
//    @Scheduled(cron = "0 0/10 * * * ?")
    private void test() throws Exception{
        System.out.println("开始时间：" + FORMAT_FOURTEEN.format(LocalDateTime.now()));
        List<String> listOrderNo = new ArrayList<>(32);
        List<RepeatParam> listOrder = repeatService.listRepeatParam(DAY.format(LocalDateTime.now()));
        List<RepeatParam> repeatParamList = listOrder.stream().filter(repeatParam -> !(repeatParam.getOrderCount().equals(1)))
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(repeatParamList) && repeatParamList.size() != 0) {
            for (RepeatParam repeatParam : repeatParamList) {
                listOrderNo.add(repeatParam.getOrderNo());
            }
            SendMailUtil.sendMail(listOrderNo.toString());
        }
    }
}
